package org.example.controller;

import org.apache.commons.lang.StringUtils;
import org.example.domain.ResponseResult;
import org.example.domain.dto.ArticleListDto;
import org.example.domain.entity.Article;
import org.example.enums.AppHttpCodeEnum;
import org.example.exception.SystemException;
import org.example.service.ArticleService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    
    private final ArticleService articleService;

    public ArticleController(ArticleService articleService) {
        this.articleService = articleService;
    }


    //  获取热门文章
    @GetMapping("/hotArticleList")
    public ResponseResult hotArticleList(){

        ResponseResult result = articleService.hotArticleList();
        return result;
    }


    //更新浏览量
    @PutMapping("/updateViewCount/{id}")
    public ResponseResult updateViewCount(@PathVariable Integer id){

        return articleService.updateViewCount(id);

    }

    //分页查询

    @GetMapping("/articleList")
    public ResponseResult articleList(@RequestBody ArticleListDto articleListDto){

        return articleService.articleList(articleListDto);

    }

    @GetMapping("/getArticleDetail/{id}")
    public ResponseResult getArticleDetail(@PathVariable Integer id){
        return articleService.getArticleDetail(id);
    }


    @GetMapping("/home/recommendArticles")
    public ResponseResult recommendArticles(){

        return articleService.recommendArticles();
    }

    @PostMapping("/getArticleList")
    public ResponseResult getArticleList(@RequestBody ArticleListDto articleListDto){

        return articleService.articleList(articleListDto);
    }

    @PostMapping("/userArticles")
    public ResponseResult userArticles(@RequestBody ArticleListDto articleListDto){

        return articleService.userArticles(articleListDto);
    }

    @PostMapping("/addArticle")
    public ResponseResult addArticle(@RequestBody Article article){
        if(StringUtils.isBlank(article.getTitle())){
            throw new SystemException(AppHttpCodeEnum.NOT_CATEGORY_NAME_ERROR);
        }
        return articleService.addArticle(article);

    }
}
